<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="listQuery.brandId" placeholder="品牌" clearable style="width: 140px" class="filter-item">
        <el-option v-for="item in brandOptions" :key="item.key" :label="item.display_name" :value="item.key" />
      </el-select>
      <el-button v-waves class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button v-waves class="filter-item" style="margin-left: 10px;" type="info" icon="el-icon-refresh" @click="handleRefresh">
        重置
      </el-button>
      <el-button v-waves :loading="exportLoading" class="filter-item" style="margin-left: 10px;" type="info" icon="el-icon-download" @click="handleExport">
        导出
      </el-button>
    </div>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="getList" />

    <el-table v-loading="listLoading" :data="list" :header-cell-style="headerCellStyle" :cell-style="{padding:'0px'}" border stripe element-loading-text="拼命加载中" />

  </div>
</template>

<script>
import { Message } from 'element-ui'
import { getNewByPage, downloadNew } from '@/api/distributor/goods-api'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { handleDownloadWithUtf8Filename } from '@/utils/index'

export default {
  name: 'DistributorGoodsNew',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      brandOptions: [],
      listQuery: {
        pageNo: 1,
        pageSize: 20,
        brandId: null
      },
      exportLoading: false,

      listLoading: true,
      list: null,
      total: 0

    }
  },
  async created() {
    await this.initLoad()
    await this.getList()
  },
  methods: {
    async initLoad() {
      this.initListQuery()
    },
    getList() {
      this.listLoading = true
      getNewByPage(this.listQuery).then(response => {
        const { data } = response
        this.list = data.result
        this.total = data.totalCount
        setTimeout(() => {
          this.listLoading = false
        }, 1 * 1000)
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
        this.listLoading = false
      })
    },
    initListQuery() {
      this.listQuery = {
        pageNo: 1,
        pageSize: 20,
        brandId: null
      }
    },

    handleFilter() {
      this.getList()
    },
    handleRefresh() {
      this.initListQuery()
      this.getList()
    },
    handleExport() {
      this.exportLoading = true
      downloadNew().then(response => {
        handleDownloadWithUtf8Filename(response)
        this.exportLoading = false
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
        this.exportLoading = false
      })
    },
    headerCellStyle({ row, rowIndex }) {
      return 'background-color: #42b983; color: #FFF;'
    }

  }
}

</script>
